﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using System.Web.UI;
using System.Text;

public partial class LoanDelaysComments : System.Web.UI.UserControl
{
    string appPath = string.Empty;
 
    int parameterX = 0;
    public int ParameterX
    {
        set { parameterX = value; }
    }

    int parameterY = 0;
    public int ParameterY
    {
        set { parameterY = value; }
    }
    
    int mLoanID = 0;
    public int LoanID
    {        
        get { return ViewState[this.ClientID + "LoanID"] != null ? (int)ViewState[this.ClientID + "LoanID"] : 0; }
        set
        {
            ViewState[this.ClientID + "LoanID"] = value; 
            commentTextBox.Text = string.Empty;
            FIllAllComments();
        }
    }

    public void FIllAllComments()
    {
        int loanID = this.LoanID;
        delayMessagesAccordion.Panes.Clear();
        if (loanID != 0)
        {
            DataTable messageGroups = Utils.ModuleCredits().GetLoanDelayMessageGroups(loanID);
            if (messageGroups != null && messageGroups.Rows.Count > 0)
            {
                for (int i = 0; i < messageGroups.Rows.Count; i++)
                {
                    int groupID = (int)messageGroups.Rows[i]["MessageGroupID"];  
                    string GroupName =  (string)messageGroups.Rows[i]["GroupName"]; 
                    DateTime groupDate = (DateTime)messageGroups.Rows[i]["Creationdate"];
                    bool activeStatus = (bool)messageGroups.Rows[i]["activeStatus"]; 

                    DataTable messagesForGroup = Utils.ModuleCredits().GetLoanDelaysComments(loanID, groupID);
                    
                    AccordionPane panel = new AccordionPane();
                    panel.ID = groupID.ToString();

                    LiteralControl contrLiteral = new LiteralControl();
                    contrLiteral.Text = "<div onclick=\"SetActivePane" + this.ClientID + "('" + (activeStatus ? groupID.ToString() : "0" ) + "')\" >" + GroupName + "  (" + groupDate.ToString(Constants.Constants.ISODateBackwardDotsFormat) + ")  " + (activeStatus ? "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href=\"#\" onclick=\"doPost('CloseActiveDiscuttion','" + groupID + "')\">Inchide Discutia activa </a>" : string.Empty) + " </div>";
                    contrLiteral.ID = "contrLiteral" + groupID.ToString();
                    panel.HeaderContainer.Controls.Add(contrLiteral);

                    LiteralControl messagesListLiteral = new LiteralControl();
                    messagesListLiteral.Text = GetHTMLTextWithMessage(messagesForGroup);
                    messagesListLiteral.ID = "messagesListLiteral" + groupID.ToString();
                    panel.ContentContainer.Controls.Add(messagesListLiteral);

                    delayMessagesAccordion.Panes.Add(panel);
                }
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {            
        RegisterJavaScript();
        if (!IsPostBack)
        {
            bodyMessageDiv.Style.Add("overflow-y", "scroll");
            bodyMessageDiv.Style.Add("padding", "5px");
        }
        else
        {
            string functionName = Request.Params.Get("__EVENTTARGET");

            if (!functionName.Equals(string.Empty))
            {
                string inputParams = Request.Params.Get("__EVENTARGUMENT");
                switch (functionName)
                {
                    case "CloseActiveDiscuttion":
                        int groupID = 0;
                        int.TryParse(inputParams, out groupID);

                        if (groupID != 0 && Utils.ModuleCredits().CloseActiveDelaysGroup(this.LoanID, groupID))
                        {
                            activePanelHiddenField.Value = string.Empty;
                            FIllAllComments();
                        }
                        else
                        {
                            Utils.InfoText(this.Page, "Atentie! Eroare in sistem!", "Datele nu au fost salvate. " + Utils.ModuleCredits().LastError);
                        }

                        break;

                    default:
                        break;
                }
            }
        }
    }

    protected void RegisterJavaScript()
    {
        if (!Page.ClientScript.IsClientScriptBlockRegistered(typeof(Page), "SetActivePane" + this.ClientID))
        {
            StringBuilder cstext2 = new StringBuilder();
            cstext2.Append("<script type=text/javascript> function SetActivePane" + this.ClientID + "(activeGroup) { \r\n ");
            cstext2.Append("  var hiddenField = document.getElementById('" + activePanelHiddenField.ClientID + "');  \r\n ");
            cstext2.Append(" if(hiddenField != null) { \r\n ");
            cstext2.Append(" if(hiddenField.value == activeGroup) { hiddenField.value = ''; } \r\n ");
            cstext2.Append(" else {hiddenField.value = activeGroup; } \r\n ");
            cstext2.Append(" } \r\n ");
            cstext2.Append("}  \r\n ");
            cstext2.Append("</script>  \r\n ");

            Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "SetActivePane" + this.ClientID, cstext2.ToString(), false);
        }
    }

    protected void SaveMessageButton_Click(object sender, EventArgs e)
    {
        string comment = commentTextBox.Text.Trim();
        int groupID = 0;
        int.TryParse(activePanelHiddenField.Value, out groupID);
        if (groupID != 0)
        {
            if (this.LoanID != 0 && !comment.Equals(string.Empty) && Utils.ModuleCredits().AddDelaysComment(this.LoanID, groupID, comment, Utils.UserObject().UserID))
            {                
                commentTextBox.Text = string.Empty;
            }
            else
            {
                Utils.InfoText(this.Page, "Atentie! Eroare in sistem!", "Datele nu au fost salvate. " + Utils.ModuleCredits().LastError);
            }
        }

        FIllAllComments();
    }

    protected string GetHTMLTextWithMessage(DataTable sourceMessagesTable)
    {
        string result = string.Empty;

        if (sourceMessagesTable != null && sourceMessagesTable.Rows.Count > 0)
        {
            for (int i = 0; i < sourceMessagesTable.Rows.Count; i++)
            {
                string userAnddateModification = sourceMessagesTable.Rows[i]["UserDate"].ToString();
                string Comment = sourceMessagesTable.Rows[i]["Comment"].ToString();

                result += "<div style=\"text-align:left; padding-left:20px;\">" + Comment + "</div>";
                result += "<div style=\"text-align:right; width:100%; float:left;\">" + userAnddateModification + "</div>";
                result += "<hr>";
                result += "<div style=\"clear:both;\"></div>";
            }
        }

        return result;
    }
    
    protected void saveGroupButton_Click(object sender, EventArgs e)
    {
        string groupName = addNewGroupTextBox.Text.Trim();

        if (Utils.ModuleCredits().AddDelaysGroup(this.LoanID, groupName))
        {
            addNewGroupTextBox.Text = string.Empty;
            FIllAllComments();
        }
        else
        {
            Utils.InfoText(this.Page, "Atentie! Eroare in sistem!", "Datele nu au fost salvate. " + Utils.ModuleCredits().LastError);
        }
    }
}
